package com.ustadzmenjawab.twitter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

import com.ustadzmenjawab.conf.Configuration;
import com.ustadzmenjawab.db.DBAccess;
import com.ustadzmenjawab.db.DBConnFactory;
import com.ustadzmenjawab.util.DateConverter;

public class Test {

	public static void main(String[] args) {
		//long ret = detectImplicitRetweet("aagym",155183099,"Amin ya Rabb RT @aagym: Selamat istrahat shbtku, teruslah berzikir, sebut nama Allah dgn lembut, penuh perasaan, ... http://t.co/n3BfQggX",);
		//System.out.println("Return: " + ret);
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 DBAccess db = new DBAccess();
		 Date dt = null;
		try {
			dt = sdf.parse("2012-02-11 06:33:33");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 String text = "3. Stlh itu kita kagum pada mrk yg pandai, yg cerdas : ilmuwan, cedekiawan, penulis, ulama, penulis dll.";
		 if(!Character.isDigit(text.charAt(0))){
				return ;
			}
			String delimiter="";
			long convid = Long.parseLong("168114840219549696");
			if(Character.isDigit(text.charAt(1))){
				if(text.charAt(2) == '.'  ){
					delimiter = ".";
				}else if(text.charAt(2) == ')'){
					delimiter = ")";
				}else if(text.charAt(2) == '>'){
					delimiter = ">";
				}
				convid =  getFirstInChainedTweet(dt,200498460,delimiter);
			}
			
			if(text.charAt(1) == '.'){
				delimiter = ".";
			}else if(text.charAt(1) == ')' ){
				delimiter = ")";
			}else if(text.charAt(1) == '>'){
				delimiter = ">";
			}
			if(delimiter.length() > 0){
				convid =  getFirstInChainedTweet(dt,200498460,delimiter); 
			}
			System.out.println("convid:"+convid);*/
		
	}
	public static long detectImplicitRetweet(String mentionedName, long mentionedUserID, String text,Date tweetCreatedAt){
		if(mentionedName == null || text == null)
			return -1;
		int idx = text.indexOf("@"+mentionedName)+mentionedName.length()+1;
		String sample = text.substring(idx+2,Math.min(idx+22, text.length())).trim();
		System.out.println(mentionedName +","+mentionedUserID + ","+sample);
		String sql = "SELECT convid FROM public_tweet " +
				" WHERE (text like ? OR userid = ?)" +
				" AND text like ? " +
				" AND DATE(created_at) = '2012-02-12' " +
				" ORDER BY created_at ASC LIMIT 1";
		DBAccess db = new DBAccess();
		Connection conn = DBConnFactory.getConnection();
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "%@"+mentionedName+"%");
			ps.setLong(2, mentionedUserID);
			ps.setString(3, "%"+sample+"%");
		
			ps.setTimestamp(4,new Timestamp(tweetCreatedAt.getTime()));
			
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				long convid = rs.getLong(1);
				System.out.println(convid);
				return convid;
			}
			return -1;
		}catch(SQLException e){
			
			e.printStackTrace();
		} finally{
			db.closeConnection(conn);
		}
		return -1;
	}
	
	public static long getFirstInChainedTweet(Date statTime, long userid,String delimiter){
		String sql = "SELECT convid,MIN(TIMEDIFF(?,created_at)) FROM public_tweet " +
				"WHERE text LIKE '1"+delimiter+"%' AND  " +
				" userid = ? AND DATE(created_at) = DATE(?)" ;
		
		Connection conn = DBConnFactory.getConnection();
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setTimestamp(1,  new Timestamp(statTime.getTime()));
			ps.setLong(2, userid);
			ps.setTimestamp(3, new Timestamp(statTime.getTime()));
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				return rs.getLong(1);
			}
			return -1;
		}catch(SQLException e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return -1;
	}
}
